/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

/*
 * Created on 13/08/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package br.ufmg.dees.insane.xyplot.xyplot_view.dialog;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.xyplot.xyplot_view.InterfaceXYPlot;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotView;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotViewState;

/**
 * @author  Samuel Silva Penna  TODO To change the template for this generated type comment go to  Window - Preferences - Java - Code Style - Code Templates
 */
public class CaptionSettingsDialog extends SimpleDialog{
	
	private static final long serialVersionUID = 1L;
	private InterfaceXYPlot parent;
    private XYPlotViewState state;
	
    private JPanel colorsTitlePane = new JPanel();
    private JTextField titleColorField = new JTextField();
    private JTextField captionTitleField = new JTextField();
    private DecimalField titleSizeDecimalField;
    private DecimalField positionXField;
    private DecimalField positionYField;
    private JComboBox titleFontCombo;
    private JComboBox titleStyleCombo;
    private JButton chooseTitleColor = new JButton("Choose");
    private String[] textFontStr;
    private String[] textStyle;
    private NumberFormat numberFormat;
    
    public CaptionSettingsDialog(InterfaceXYPlot parent, ViewState s) {
        this.parent = parent;
        state = (XYPlotViewState)s;
        setUpFormat();
        
        //Sets up the main panel
        JPanel mainPane = new JPanel();
        mainPane.setLayout(new GridLayout(1,4,2,2));          
        mainPane.setPreferredSize(new Dimension(300,225));
        mainPane.setMaximumSize(new Dimension(300,225));
        mainPane.setLayout(new BoxLayout(mainPane,BoxLayout.Y_AXIS));      
        
        //set up the titlePane
        JPanel titlePane = new JPanel();
        titlePane.setLayout(new GridLayout(4,2,2,2));          
        titlePane.setPreferredSize(new Dimension(300,125));
        titlePane.setMaximumSize(new Dimension(300,125));
        titlePane.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder("Caption Title"),
        		BorderFactory.createEmptyBorder(2,2,2,2)));
        // title field 
        titlePane.add(new JLabel("Title:"));
        captionTitleField.setMaximumSize(new Dimension(75,26));
        captionTitleField.setPreferredSize(new Dimension(75,26));
        captionTitleField.setMinimumSize(new Dimension(75,26));
        captionTitleField.setSize(new Dimension(75,26));  
        captionTitleField.setText(state.getCaptionState().getTitle());
        titlePane.add(captionTitleField);
        // title font
        textFontStr = new String[5];
        textFontStr[0] = "Arial";
        textFontStr[1] = "Monospaced";
        textFontStr[2] = "SansSerif";
        textFontStr[3] = "Time"; 
        textFontStr[4] = "NONE"; 
        titleFontCombo = new JComboBox(textFontStr);
        titlePane.add(new JLabel("Title font:"));
        titleFontCombo.setMaximumSize(new Dimension(75,26));
        titleFontCombo.setPreferredSize(new Dimension(75,26));
        titleFontCombo.setMinimumSize(new Dimension(75,26));
        titleFontCombo.setSize(new Dimension(75,26));  
        titleFontCombo.setSelectedItem(state.getCaptionState().getTitleFont());
        titlePane.add(titleFontCombo); 
        // title font size
        titlePane.add(new JLabel("Size"));
        titleSizeDecimalField = new DecimalField(state.getCaptionState().getTitleSize(),10,numberFormat);
        titleSizeDecimalField.setMaximumSize(new Dimension(75,26));
        titleSizeDecimalField.setPreferredSize(new Dimension(75,26));
        titleSizeDecimalField.setMinimumSize(new Dimension(75,26));
        titleSizeDecimalField.setSize(new Dimension(75,26));  
        titleSizeDecimalField.setEditable(true);
        titlePane.add(titleSizeDecimalField);
       // title font Style
        textStyle = new String[3];
        textStyle[0] = "Plain";
        textStyle[1] = "Bold";
        textStyle[2] = "Italic";
        titleStyleCombo = new JComboBox(textStyle);
        titlePane.add(new JLabel("Font Style:"));
        titleStyleCombo.setMaximumSize(new Dimension(75,26));
        titleStyleCombo.setPreferredSize(new Dimension(75,26));
        titleStyleCombo.setMinimumSize(new Dimension(75,26));
        titleStyleCombo.setSize(new Dimension(75,26));  
        titleStyleCombo.setSelectedIndex(state.getCaptionState().getTitleStyle());
        titlePane.add(titleStyleCombo); 
        
//      Setting subpanel colors Title panel 
        colorsTitlePane = new JPanel();
        colorsTitlePane.setLayout(new BoxLayout(colorsTitlePane, BoxLayout.Y_AXIS));
        colorsTitlePane.setMaximumSize(new Dimension(300,45));
        colorsTitlePane.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder(""),
        		BorderFactory.createEmptyBorder(2,2,2,2)));
        colorsTitlePane.setLayout(new GridLayout(1,3,3,3));
        colorsTitlePane.add(new JLabel("Title Color"));
        titleColorField = new JTextField();
        titleColorField.setEditable(false);
        titleColorField.setBackground(state.getCaptionState().getTitleColor());
        colorsTitlePane.add(titleColorField);
        chooseTitleColor = new JButton("Choose");
        colorsTitlePane.add(chooseTitleColor);
        chooseTitleColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                   titleColorActionPerformed(e);
                }
            } );
        
        JPanel positionPane = new JPanel();
        positionPane.setLayout(new GridLayout(1,2,2,2));          
        positionPane.setMaximumSize(new Dimension(350,60));
        positionPane.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder("Position"),
        		BorderFactory.createEmptyBorder(2,2,2,2)));  
        
        // title position X  
        JPanel positionXPanel = new JPanel();
        positionXPanel.setLayout(new GridLayout(1,2,2,2));  
        positionXPanel.setMaximumSize(new Dimension(75,26));
        positionXPanel.setPreferredSize(new Dimension(75,26));
        positionXPanel.setMinimumSize(new Dimension(75,26));
        positionXPanel.setSize(new Dimension(75,26));  
        positionXPanel.add(new JLabel("Position X"));
        positionXField = new DecimalField(state.getCaptionState().getXPosition(),10,numberFormat);
        positionXField.setMaximumSize(new Dimension(75,26));
        positionXField.setPreferredSize(new Dimension(75,26));
        positionXField.setMinimumSize(new Dimension(75,26));
        positionXField.setSize(new Dimension(75,26));  
        positionXField.setValue(state.getCaptionState().getXPosition());
        positionXPanel.add(positionXField);
        positionPane.add(new JLabel("Position X"));
        positionPane.add(positionXField);
        // title position Y
        JPanel positionYPanel = new JPanel();
        positionYPanel.setLayout(new GridLayout(1,2,2,2));  
        positionYPanel.setMaximumSize(new Dimension(75,26));
        positionYPanel.setPreferredSize(new Dimension(75,26));
        positionYPanel.setMinimumSize(new Dimension(75,26));
        positionYPanel.setSize(new Dimension(75,26));  
        positionYPanel.add(new JLabel("Position Y"));
        positionYField = new DecimalField(state.getCaptionState().getYPosition(),10,numberFormat);
        positionYField.setMaximumSize(new Dimension(75,26));
        positionYField.setPreferredSize(new Dimension(75,26));
        positionYField.setMinimumSize(new Dimension(75,26));
        positionYField.setValue(state.getCaptionState().getYPosition());
        positionYPanel.add(positionYField);
        positionPane.add(new JLabel("Position Y"));
        positionPane.add(positionYField);
       
        mainPane.add(positionPane);
        mainPane.add(titlePane);
        mainPane.add(colorsTitlePane);
        
        
        setPane(mainPane);
    }
    
    public int showDialog() {
        return super.showDialog(parent, "Caption Settings");
    }
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
			
        	state.getCaptionState().setTitle((String)this.captionTitleField.getText());
			state.getCaptionState().setTitleFont((String)this.titleFontCombo.getSelectedItem());
			state.getCaptionState().setTitleSize((int)this.titleSizeDecimalField.getValue());
			state.getCaptionState().setTitleStyle((byte)this.titleStyleCombo.getSelectedIndex());
			state.getCaptionState().setXPosition((double)this.positionXField.getValue());
			state.getCaptionState().setYPosition((double)this.positionYField.getValue());
			state.getCaptionState().setTitleColor(this.titleColorField.getBackground());
			
			//parent.setModel(parent.getModel());
            this.setVisible(false);
            ((XYPlotView)parent.getView()).getDrawingArea().repaint(); 
		//	LastZoomCommand cmd = new LastZoomCommand(parent);
		//	cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
    private void titleColorActionPerformed(ActionEvent e) {
        titleColorField.setBackground(JColorChooser.showDialog(this, "Choose Title Color", titleColorField.getBackground()));
    }
    
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(10);
    }

}
